package com.googlecode.gwt.test.plugin.editors;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.swt.SWT;

public class GWTCsvScanner extends RuleBasedScanner  {

	ColorManager colorManager ; 

	KeyWords keyWords;
	
	public GWTCsvScanner(ColorManager manager, KeyWords keyWords) {
		this.colorManager= manager;
		this.keyWords = keyWords;
		setRules(extractRules()); 
	}

	public void update(){
		setRules(extractRules());
	}

	private IRule[] extractRules() {
		IToken keyword = new Token(new TextAttribute(colorManager.getColor(GWTColorConstants.KEYWORD), null, SWT.BOLD));
		IToken comment = new Token(new TextAttribute(colorManager.getColor(GWTColorConstants.COMMENT), null, SWT.ITALIC));
		IToken defaut = new Token(new TextAttribute(colorManager.getColor(GWTColorConstants.DEFAULT)));
		IToken custom_keyword = new Token ( new TextAttribute(colorManager.getColor(GWTColorConstants.CUSTOM_KEYWORD),null,SWT.BOLD));
		IToken custom_macro = new Token ( new TextAttribute(colorManager.getColor(GWTColorConstants.CUSTOM_MACROS),null,SWT.BOLD|SWT.ITALIC));
		IToken comma_dot = new Token ( new TextAttribute(colorManager.getColor(GWTColorConstants.COMMA_DOT),colorManager.getColor(GWTColorConstants.COMMA_DOT_BG),SWT.BOLD));
		IToken special = new Token ( new TextAttribute(colorManager.getColor(GWTColorConstants.SPECIAL_KEYWORDS),null,SWT.BOLD));
		
		
		WordRule rule = new WordRule(new IWordDetector() {
			@Override
			public boolean isWordPart(char c) {
				return Character.isJavaIdentifierPart(c) ;
			}

			@Override
			public boolean isWordStart(char c) {
				return Character.isJavaIdentifierStart(c) ;
			}
		}, defaut);
		

		for (String k : keyWords.getKEYWORDS()) {
            rule.addWord(k, keyword);
		}
		for (String k : keyWords.getCustomKeywords()) {
            rule.addWord(k, custom_keyword);
		}
		for (String k : keyWords.getCUSTOM_MACROS()) {
            rule.addWord(k, custom_macro);
		}
		
		for(String k: keyWords.getSpecialKeyWords()){
			rule.addWord(k, special);
		}

		IRule [] rules = new IRule[3];
		rules[0]=rule;
		rules[1]=new SingleLineRule("**", null, comment, (char) 0, true);
		IRule r=new WordRule(new IWordDetector() {
			@Override
			public boolean isWordPart(char c) {
				return c == ';' ;
			}

			@Override
			public boolean isWordStart(char c) {
				return c == ';' ;
			}
		},comma_dot);
		rules[2]=r;
		return rules;
	}


	
}
